<?php

namespace Wechat\Controller;

class OrderController extends CommonController {

    public function index() {

        $id = $_REQUEST["id"];

        $product = D("Product")->queryById($id);

        if(isNullOrEmpty($product)) {
            $this->redirect(U("product/index"));
        }

        $category = D("Category")->queryById($product["category_id"]);

        if($category["is_finished"] != C("DEFAULT_YN.YES")) {
            $this->redirect(U("product/index"));
        }

//        if($category["need_doctor"] == 1) {
//
//            if(isNullOrEmpty($this->customer["doctor_name"])
//                || isNullOrEmpty($this->customer["hospital_name"])) {
//                $this->assign("confirmInfo", 1);
//            }
//
//        }
        $this->assign("category", $category);
        //产品是否属于病例资料中所选病症
        if($product["category_id"] == $this->customer["category_id"])
            $this->assign("customer", $this->customer);

        $this->assign("product", $product);
        $this->display("index");

    }

    public function measureInfo() {

        $id = $_REQUEST["id"];

        $orderMap["customer_id"] = $this->customer["customer_id"];
        $orderMap["order_id"] = $id;

        $order = D("Order")->queryByMap($orderMap);

        if(isNullOrEmpty($order)) {
            $this->redirect(U("userCenter/index"));
        }

        $product = D("Product")->queryById($order["product_id"]);

        $order["measure_data"] = json_decode(html_entity_decode($order["measure_data"]) ,true);

        $this->assign("product", $product);
        $this->assign("order", $order);
        $this->display("measureInfo");

    }

    public function measure() {

        $id = $_REQUEST["id"];

        $product = D("Product")->queryById($id);

        if(isNullOrEmpty($product)) {
            $this->redirect(U("product/index"));
        }

        $category = D("Category")->queryById($product["category_id"]);

        if($category["is_finished"] != C("DEFAULT_YN.YES")) {
            $this->redirect(U("product/index"));
        }

        if($product["need_measure"] != C("DEFAULT_YN.YES")) {
            $this->redirect(U("order/index", array("id"=>$id)));
        }

        $product["measure_json"] = unserialize($product["measure_json"]);

        $this->assign("product", $product);
        $this->display("measure");

    }

    public function createOrder() {

        $productId = $_REQUEST["productId"];
        $product = D("Product")->queryById($productId);

        $data["code"] = 200;

        if(isNullOrEmpty($product)) {
            $data["code"] = -1;
            $data["msg"] = "商品不存在";
            echo json_encode($data);
            exit();
        }

        $category = D("Category")->queryById($product["category_id"]);

        if($category["is_finished"] != C("DEFAULT_YN.YES")) {
            $data["code"] = -2;
            $data["msg"] = "商品不是成品，请预约";
            echo json_encode($data);
            exit();
        }

        $orderData["create_time"] = getCurrentDate();
        $orderData["product_id"] = $productId;
        $orderData["customer_id"] = $this->customer["customer_id"];
        $orderData["need_invoice"] = I("post.needInvoice");
        $orderData["invoice_name"] = I("post.invoiceName");
        $orderData["invoice_number"] = I("post.invoiceNumber");
        $orderData["receive_name"] = I("post.receiveName");
        $orderData["receive_address"] = I("post.receiveAddress");
        $orderData["receive_contact"] = I("post.receiveContact");
        $orderData["number"] = I("post.number");
        if($product["category_id"] == 2){
            $orderData["price"] = $product["price"]*$orderData["number"] - $this->customer["point"];
        }else{
            $orderData["price"] = $product["price"]*$orderData["number"];
        }

        $orderData["note"] = I("post.note");
        $orderData["delivery_type"] = I("post.deliveryType");
        $orderData["need_measure"] = I("post.needMeasure");
        $orderData["measure_data"] = I("post.measureData");

        if($product["product_id"] == 2) {
            $measureData = json_decode(html_entity_decode($orderData["measure_data"]) ,true);
            $orderData["measure_result"] = calculateIowaData($measureData[0]["value"]);
        } elseif($product["product_id"] == 5) {
            $measureData = json_decode(html_entity_decode($orderData["measure_data"]) ,true);
            $orderData["measure_result"] = calculateDRAFO(
                $measureData[0]["value"], $measureData[2]["value"],
                $measureData[1]["value"]);

        }

        $payId = D('Pay')->add(array(
            'out_trade_no' => time() . rand(0, 999),
            'money' => $orderData["price"],
            'status' => 0,
            'type' => 1,
            'callback' => "",
            'create_time' => getCurrentDate(),
            'update_time' => getCurrentDate(),
            'pay_source' => 1
        ));

        $orderData["pay_id"] = $payId;

        $orderId = D("Order")->add($orderData);

        //该产品是否需要医生
        if($category["need_doctor"] == 1) {
            $customerInfo = D("Customer")->queryById($this->customer["customer_id"]);
            $customerInfo["category_id"] = $product["category_id"];
            $customerInfo["doctor_name"] = I("post.doctorName");
            $customerInfo["hospital_name"] = I("post.hospitalName");
            D("Customer")->updateInfo($customerInfo);
        }
        $customerInfo = D("Customer")->queryById($this->customer["customer_id"]);
        if($product["product_id"] == 2){
            $customerInfo["point"] = 0;
        }

        D("Customer")->updateInfo($customerInfo);
        $data["order_id"] = $orderId;

        echo json_encode($data);

    }

}